package someTestExcemple.ioStream;

import java.io.FileWriter;
import java.io.Writer;

public class FileWriterTest1 {
    public static void main(String[] args) {
    test1();
    }
    public static void test1() {
        //文件字符输出流
        try(
                //追加方式写入数据
                Writer writer =
                        new FileWriter("src/main/java/someTestExcemple/ioStream/fileWriterTest1.txt",true);

        ) {
            writer.write("hello world");
            writer.write("\r\n");
            writer.write("我爱写java",0,3);
            char[] chars = {'如','j','a','v','a'};
            writer.write(chars);
            //注意字符输出流写入数据后，必须要刷新流或者关闭流，数据才能生效
            //因为它是先写到缓冲区，然后才写入到文件，缓冲流满了，下次写入的时候会自动刷新
            //这里用了try-with-resource，()里面会自动关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
